var socket = io.connect();
(function($){
    var isAdmin;
    var inv = [];
    var invPlayer = [];
    window.onload = function(){
        socket.emit('isAlliOwner');
        socket.emit('requestInvit');
        socket.emit('requestInvitPlayer');

        socket.on('isAlliOwnerRespond', function(ad){
            isAdmin = ad;
        });

        socket.on('youInvit', function(myInvit){
            inv = myInvit;
            if(isAdmin){
                for ( var i = 0; i< inv.length; i++ ){
                    $(".player").append("<tr><td>"+ inv[i].name_invited+"</td><td>Notaccepted</td><td><input type=\"button\" onClick=sprInv(\""+ inv[i].name_invited +"\"); value=\"Supprimer cette invitation\"></td></tr>");
                }
            }else{
                for ( var i = 0; i< inv.length; i++ ){
                    $(".player").append("<tr><td>"+ inv[i].name_invited+"</td></tr>");
                }
            }
        });

        socket.on('sendInvitPlayer', function(inv){
            invPlayer = inv;
            for ( var i = 0; i< inv.length; i++ ){
                $(".invit").append("<tr><td>"+ inv[i].alliance_name+"</td><td><input type=\"button\" onClick=accInv(\""+ inv[i].alliance_name +"\"); value=\"Accepter cette invitation\"></td><td><input type=\"button\" onClick=refInv(\""+ inv[i].alliance_name +"\"); value=\"Refuser cette invitation\"></td></tr>");
            }
        });
    }

    $('#createAllForm').submit(function(event){
        event.preventDefault();
        socket.emit('createAlliance', {
            allName              : $('#allName').val()
        });
    });

    socket.on('allianceCreatingOK', function(){
        document.location.reload(true);
    });

    socket.on('allianceNameAlreadyTaken', function(){
        $('#allName').val('');
        window.alert("Le nom de l'alliance est deja pris ");
    });

    $('#ragequitAlli').submit(function(event){
        event.preventDefault();
        socket.emit('ragequitAlli');
    });

    socket.on('ragequitok', function(){
       window.alert('Vous venez bien de quitter votre alliance');
        document.location.reload(true);
    });

    $('#invitePF').submit(function(event){
        event.preventDefault();
        socket.emit('invitPlayer', {
            pName: $('#PName').val()
        })
    });

    socket.on('invitOK', function(){
        window.alert('Invitation envoyée ');
        document.location.reload(true);
    });

    socket.on('suprInvitOK', function(){
        window.alert('Invitation supprimée');
        document.location.reload(true);
    });

    socket.on('playerAlreadyInvit', function(){
        window.alert('Player already invited');
    });

    socket.on('refInvitOK', function(){
       document.location.reload(true);
    });

    socket.on('accInvitOK', function(){
        document.location.reload(true);
    })

})(jQuery);

function sprInv(user){
    socket.emit('suprInvit', {
        usr: user
    });
}

function refInv(allName){
    socket.emit('refInvit', {
         allN : allName
    })
}

function accInv(allName){
    socket.emit('accInvit', {
        allN: allName
    })
}